<?php
	
class Calendario{
	
	var $id;
	var $data;
	var $titulo;
	var $escola_id;

	/* Getters and Setters */
	public function setId($value){
		$this->id = $value;
	}
	public function getId(){
		return $this->id;
	}
	
   	public function setData($value){
		$this->data = $value;
	}
	public function getData(){
		return $this->data;
	}
	
   	public function setTitulo($value){
		$this->titulo = $value;
	}
	public function getTitulo(){
		return $this->titulo;
	}
	
   	public function setEscola_id($value){
		$this->escola_id = $value;
	}
	public function getEscola_id(){
		return $this->escola_id;
	}
	/* Getters and Setters */

	
		
	function gridCalendario($page,$limit,$sidx,$sord){
		
		$con = new Conexao();		
		
		$page  = $page; 
		$limit = $limit; 
		$sidx  = $sidx; 
		$sord  = $sord; 
			
		$queryCount = "SELECT COUNT(id_calendario) as count
					   from calendario
					   WHERE escola_id = '".$_SESSION['escola_id']."'";
					 
		$resultSetCount = $con->query($queryCount);			 
					 
		$rowCount = $con->fetchArray($resultSetCount);
		$count = $rowCount['count'];
		
		if( $count>0 ){
			$total_pages = ceil($count/$limit);	
		}else{
			$total_pages = 0;
		}
		
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit;
		
		$query = "SELECT id_calendario,
						 titulo,
						 DATE_FORMAT(data, '%e/%m/%Y') as data
				  FROM calendario
				  WHERE escola_id = '".$_SESSION['escola_id']."'
				  ORDER BY $sidx $sord 
				  LIMIT $start , $limit";				 
				 // echo $query;die;
						
		$resultSet = $con->query($query);
		
		$response->page = $page;
		$response->total = $total_pages;
		$response->records = $count;
		$i=0;
		
		while ( $rowCalendario = $con->fetchArray($resultSet) ){
							
				$response->rows[$i]['id']=$rowCalendario['id_calendario'];
				$response->rows[$i]['titulo']=$rowCalendario['titulo'];
				$response->rows[$i]['data']=$rowCalendario['data'];
				$i++;
					
		}	
		
		return $response;
	}


	function salvar($object){
		
		$con = new Conexao();
		 	 		 
		$query2 = "INSERT INTO calendario (
					   titulo,
					   escola_id,
					   data)
				   VALUES(
					   '".$object->titulo."',
					   '".$object->escola_id."',
					   '".$object->data."')";
				 
	    return $con->query($query2);
	
	}
	
	function atualizar($object){
		
		$con = new Conexao();
		
		$query2 = "UPDATE calendario SET
						  titulo = '".$object->titulo."',
						  data = '".$object->data."'
				   WHERE id_calendario = ".$object->id."";
				 
	    return $con->query($query2);
		
	}
	
	function deletar($object){
		
		$con = new Conexao();
		
		$query2 = "DELETE FROM calendario WHERE id_calendario = ".$object->id."";
				 
	    return $con->query($query2);
		
	}
	
	
	function consultaDiaCalendario($data){
		
	   $con = new Conexao();
		
	   $query = "SELECT id_calendario,
	   					escola_id,
					    data,
					    titulo
				 FROM calendario
				 WHERE data = '".$data."'";
				 
	   $result = $con->query($query);
	   
	   return $con->fetchArray($result);
	   
	}
	
	
	function exibeDatasCalendario($escola_id){
		
	   $con = new Conexao();
		
	   $query = "SELECT id_calendario,
					    DATE_FORMAT(data, '%Y%m%e') as data
				 FROM calendario
				 WHERE escola_id = '".$escola_id."'
				 AND DATE_FORMAT(data, '%Y') = '".date('Y')."'";
				 
	   $result = $con->query($query);
	   
	   $datasAno = "";
	   
	   while( $row = $con->fetchArray($result) ){
			$datasAno .= "'".$row["data"]."',";   
	   } 
	   
	   return rtrim($datasAno,",");
	}
}

?>